import matplotlib.pyplot as plt
import numpy as np

rng = np.arange(50)  # 0 - 49
# 0~10 之间的随机数，形状为 (3,50)
rnd = np.random.randint(0, 10, size=(5, rng.size))
print(rng.shape)  # (50,)
print(rnd.shape)  # (3,50)
yrs = 1950 + rng  # 1950~1999

fig, ax = plt.subplots(figsize=(5, 3))
print(rng + rnd)
ax.stackplot(yrs, rng + rnd, labels=['Eastasia', 'Eurasia', 'Oceania'])
ax.set_title('Combined debt growth over time')

ax.legend(loc='upper left')
ax.set_ylabel('Total debt')

ax.set_xlim(xmin=yrs[0], xmax=yrs[-1])
fig.tight_layout()  # 应用于整个Figure对象来清理空白填充。
# plt.show()


x = np.random.randint(low=1, high=11, size=50)
y = x + np.random.randint(1, 5, size=x.size)
data = np.column_stack((x, y))

# nrows=1 要创建的子图的行数, ncols=2 要创建的子图的列数
fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(8, 4))
"""
    scatter 分散;绘制散点图;x=x y=y 指定散点图的 x 轴和 y 轴的数据
    marker: 标记 'o'圆, 's'正方形, '^'三角形 , 'D'菱形
    c: 点的颜色
    edgecolor: 边界颜色
"""
ax1.scatter(x=x, y=y, marker='o', c='r', edgecolor='b')
ax1.set_title('Scatter: $x$ versus $y$')  # $x$ $y$ 用于显示数学式子样式的文本
ax1.set_xlabel('$x$')
ax1.set_ylabel('$y$')

"""
    hist: 绘制直方图
    data: 要绘制的数据 
    
"""
ax2.hist(data, bins=np.arange(data.min(), data.max()), label=('x', 'y'))
ax2.legend(loc=(0.65, 0.8))
ax2.set_title('Frequencies of $x$ and $y$')
ax2.yaxis.tick_right()  # 设置y坐标轴刻度及刻度标签在右边

plt.show()
figure, axes = plt.subplots(nrows=2, ncols=2, figsize=(5, 3))
print(type(axes))  # numpy.ndarray
print(axes.shape)  # (2, 2)
ax1, ax2, ax3, ax4 = ax.flatten()  # 把数组展开
